import 'package:core_package/core_package.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/model/GetFirstInviteListItem.dart';
import 'package:flutter_app_unionad/model/asset_download_model.dart';
import 'package:flutter_app_unionad/model/promotion_materials_model.dart';
import 'package:flutter_app_unionad/model/share_pic_model.dart';
import 'package:flutter_app_unionad/model/take_save_model.dart';
import 'package:flutter_app_unionad/model/task_item_model.dart';
import 'package:flutter_app_unionad/request/base_request.dart';

class PromotionRequest {
  static Future<List<TaskItemModel>?> getTakeList() async {
    final future = await HttpRequest.getRequest('/api/gameTask/list', params: {
      'packageName': 'com.niuniu.app',
      'status': '0',
      'type': '3',
    });
    List<TaskItemModel>? list;
    baseRequestAnalysisData(future, (map) {
      list = [];
      for (var v in map['list']) {
        list?.add(TaskItemModel.fromJson(v));
      }
    }, (info, code) {});
    return list;
  }

  static Future<TaskSaveModel?> saveGameTake({
    @required TaskItemModel? itemModel,
  }) async {
    Map<String, dynamic> map = {
      'userId': UserCacheUil.getInt(Configuration.userId),
      'appId': itemModel!.appId,
      'taskId': itemModel.id,
      'completeCount': itemModel.completeCount,
      'dateCap': itemModel.dateCap,
      'id': 0,
      'lastCompleteDate': itemModel.needCompleteCount,
      'totalCount': itemModel.maxCount,
      'totalPoint': itemModel.point,
      'updateTime': itemModel.updateTime,
    };
    TaskSaveModel? model;
    final future = await HttpRequest.postRequest('/api/userGameTask/save', params: {
      'userGameTaskList': [map],
    });

    baseRequestAnalysisData(future, (map) {
      model = TaskSaveModel.fromJson(map);
    }, (info, code) {});
    return model;
  }

  static Future<SharePicModel?> getShareInfo({
    int type = 3,
  }) async {
    final future = await HttpRequest.getRequest('/api/picture/getByType', params: {'type': type});
    SharePicModel? item;
    baseRequestAnalysisData(future, (map) {
      if (map != null) item = SharePicModel.fromJson(map);
    }, (info, code) {});
    return item;
  }

  static Future<List<PromotionMaterialsModel>?> getListByType({
    int? type, //1-开屏广告; 2-推广页面; 3-合伙人推荐; 4-素材下载
    int? pageSize = 20,
    int? pageNum =1,
  }) async {
    final future = await HttpRequest.postRequest(
      '/api/promotionMaterials/getListByType',
      params: {
        'type': type,
        'pageSize': pageSize,
        'pageNum': pageNum,
      },
    );
    print('future  --- $future');
    List<PromotionMaterialsModel>? list;
    baseRequestAnalysisData(future, (future) {
      list = [];
      for (var i in future['list']) {
        list?.add(PromotionMaterialsModel.fromJson(i));
      }
    }, (info, code) {});
    return list;
  }

  static Future<TaskSaveModel?> getUserAccountAmount() async {
    final future = await HttpRequest.getRequest('/api/userGameTask/getUserPointData', params: {
      'openId': UserCacheUil.getString(Configuration.openid),
    });
    TaskSaveModel? item;
    baseRequestAnalysisData(future, (map) {
      //
      item = TaskSaveModel.fromJson(map);
    }, (info, code) {
      //
    });
    return item;
  }

  static Future<bool> exchangePoint({
    String? amount,
    int type = 1, // 0提现，1兑换天
    String? devId,
  }) async {
    Map<String, dynamic> map = {'amount': amount};
    if (devId != null && devId.stringIsNotEmpty) map["devId"] = devId;

    final future = await HttpRequest.postRequest('/api/lingXinPay/pointExchangeDays', params: map);
    bool result = false;
    baseRequestAnalysisData(future, (map) {
      //
      ShowToastWidget.showToast(msg: '兑换成功');
      result = true;
    }, (info, code) {
      //
      result = false;
      ShowToastWidget.showToast(msg: info);
    });
    return result;
  }

  static Future<List<GetFirstInviteListItem>> getFirstInviteList({
    int? pageNum,
    int pageSize = 20,
    int type = 0, //0 推广订单 1团队销售
  }) async {
    final future = await HttpRequest.postRequest(
      type == 0 ? '/api/booking/getFirstInviteList' : '/api/booking/getFirstAndSecondInviteList',
      params: {
        'pageNum': pageNum,
        'pageSize': pageSize,
      },
    );
    List<GetFirstInviteListItem> list = [];
    baseRequestAnalysisData(future, (data) {
      for (var map in data['list']) {
        list.add(GetFirstInviteListItem.fromJson(map));
      }
    }, (info, code) {});

    return list;
  }

  static Future<List<AssetsDownloadModel>> getAssetsDownloadList({
    int? pageNum,
    int pageSize = 20,
  }) async {
    final future = await HttpRequest.postRequest(
      '/api/promotionMaterials/getList',
      params: {
        'pageNum': pageNum,
        'pageSize': pageSize,
      },
    );
    List<AssetsDownloadModel> list = [];
    baseRequestAnalysisData(future, (data) {
      for (var map in data['list']) {
        list.add(AssetsDownloadModel.fromJson(map));
      }
    }, (info, code) {});

    return list;
  }
}
